package cn.iocoder.yudao.module.ao.controller.admin.schedule;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
// import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
// import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

// import javax.validation.constraints.*;
import javax.validation.*;
// import javax.servlet.http.*;
import java.util.*;
// import java.io.IOException;

// import cn.iocoder.yudao.framework.common.pojo.PageParam;
// import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
// import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

// import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

// import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
// import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.schedule.vo.*;
// import cn.iocoder.yudao.module.ao.dal.dataobject.schedule.ScheduleDO;
import cn.iocoder.yudao.module.ao.service.schedule.ScheduleService;

@Tag(name = "管理后台 - 课程安排")
@RestController
@RequestMapping("/system/schedule")
@Validated
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;

    /* 
     * ============================================================
     * 以下方法已废弃 - 不适用于新的 jx_senior_vocational_class_schedule 表结构
     * ============================================================
     */

    // @PostMapping("/create")
    // @Operation(summary = "创建课程安排")
    // @PreAuthorize("@ss.hasPermission('ao:schedule:create')")
    // public CommonResult<Long> createSchedule(@Valid @RequestBody ScheduleSaveReqVO createReqVO) {
    //     return success(scheduleService.createSchedule(createReqVO));
    // }

    // @PutMapping("/update")
    // @Operation(summary = "更新课程安排")
    // @PreAuthorize("@ss.hasPermission('ao:schedule:update')")
    // public CommonResult<Boolean> updateSchedule(@Valid @RequestBody ScheduleSaveReqVO updateReqVO) {
    //     scheduleService.updateSchedule(updateReqVO);
    //     return success(true);
    // }

    // @DeleteMapping("/delete")
    // @Operation(summary = "删除课程安排")
    // @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('ao:schedule:delete')")
    // public CommonResult<Boolean> deleteSchedule(@RequestParam("id") Long id) {
    //     scheduleService.deleteSchedule(id);
    //     return success(true);
    // }

    // @DeleteMapping("/delete-list")
    // @Parameter(name = "ids", description = "编号", required = true)
    // @Operation(summary = "批量删除课程安排")
    // @PreAuthorize("@ss.hasPermission('ao:schedule:delete')")
    // public CommonResult<Boolean> deleteScheduleList(@RequestParam("ids") List<Long> ids) {
    //     scheduleService.deleteScheduleListByIds(ids);
    //     return success(true);
    // }

    // @GetMapping("/get")
    // @Operation(summary = "获得课程安排")
    // @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('ao:schedule:query')")
    // public CommonResult<ScheduleRespVO> getSchedule(@RequestParam("id") Long id) {
    //     ScheduleDO schedule = scheduleService.getSchedule(id);
    //     return success(BeanUtils.toBean(schedule, ScheduleRespVO.class));
    // }

    // @GetMapping("/page")
    // @Operation(summary = "获得课程安排分页")
    // @PreAuthorize("@ss.hasPermission('ao:schedule:query')")
    // public CommonResult<PageResult<ScheduleRespVO>> getSchedulePage(@Valid SchedulePageReqVO pageReqVO) {
    //     PageResult<ScheduleDO> pageResult = scheduleService.getSchedulePage(pageReqVO);
    //     return success(BeanUtils.toBean(pageResult, ScheduleRespVO.class));
    // }

    // @GetMapping("/export-excel")
    // @Operation(summary = "导出课程安排 Excel")
    // @PreAuthorize("@ss.hasPermission('ao:schedule:export')")
    // @ApiAccessLog(operateType = EXPORT)
    // public void exportScheduleExcel(@Valid SchedulePageReqVO pageReqVO,
    //           HttpServletResponse response) throws IOException {
    //     pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    //     List<ScheduleDO> list = scheduleService.getSchedulePage(pageReqVO).getList();
    //     // 导出 Excel
    //     ExcelUtils.write(response, "课程安排.xls", "数据", ScheduleRespVO.class,
    //                     BeanUtils.toBean(list, ScheduleRespVO.class));
    // }

    // @GetMapping("/table")
    // @Operation(summary = "获取课程表")
    // public CommonResult<List<ScheduleRespVO>> getScheduleTable(
    //         @RequestParam(value = "location", required = false) String location,
    //         @RequestParam("weekNumber") Integer weekNumber,
    //         @RequestParam(value = "teacherName", required = false) String teacherName) {
    //     List<ScheduleDO> list = scheduleService.getScheduleTable(location, weekNumber, teacherName);
    //     return success(BeanUtils.toBean(list, ScheduleRespVO.class));
    // }

    // @PostMapping("/check-conflict")
    // @Operation(summary = "检查时间冲突")
    // public CommonResult<ScheduleConflictCheckRespVO> checkConflict(@Valid @RequestBody ScheduleConflictCheckReqVO reqVO) {
    //     return success(scheduleService.checkConflict(reqVO));
    // }

    @GetMapping("/locations")
    @Operation(summary = "获取所有上课地点")
    public CommonResult<List<String>> getLocationList() {
        return success(scheduleService.getLocationList());
    }

    @GetMapping("/teacher-schedule")
    @Operation(summary = "获取教师个人课表")
    public CommonResult<List<TeacherScheduleRespVO>> getTeacherSchedule(
            @RequestParam("teacherName") String teacherName,
            @RequestParam(value = "term", required = false) String term) {
        return success(scheduleService.getTeacherSchedule(teacherName, term));
    }

    @PutMapping("/update-course")
    @Operation(summary = "更新课程内容（支持新增和编辑）")
    public CommonResult<Boolean> updateCourseContent(
            @Valid @RequestBody UpdateCourseReqVO reqVO,
            @RequestParam("teacherName") String teacherName) {
        
        if (reqVO.getId() == null) {
            // 新增课程
            if (reqVO.getClassName() == null || reqVO.getClassSection() == null || reqVO.getClassroom() == null) {
                throw new RuntimeException("新增课程时，班级、时间段、教室不能为空");
            }
            scheduleService.addNewCourseContent(
                reqVO.getClassName(),
                reqVO.getClassSection(),
                reqVO.getDayOfWeek(),
                reqVO.getClassroom(),
                reqVO.getCourseContent(),
                teacherName
            );
        } else {
            // 编辑课程
            scheduleService.updateCourseContent(
                reqVO.getId(), 
                reqVO.getDayOfWeek(), 
                reqVO.getCourseContent(), 
                teacherName
            );
        }
        return success(true);
    }

    @GetMapping("/check-time-conflict")
    @Operation(summary = "检查时间段和教室是否冲突")
    public CommonResult<String> checkTimeConflict(
            @RequestParam("classSection") String classSection,
            @RequestParam("classroom") String classroom,
            @RequestParam("dayOfWeek") Integer dayOfWeek,
            @RequestParam(value = "excludeId", required = false) Long excludeId) {
        String conflictTeacher = scheduleService.checkTimeConflict(classSection, classroom, dayOfWeek, excludeId);
        return success(conflictTeacher);
    }

}